package servlet;

import com.alibaba.fastjson.JSON;
import pojo.User;
import service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/userRegisterServlet")
public class UserRegisterServlet extends HttpServlet {
    private final UserService userService = new UserService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader bufferedReader = request.getReader();
        String json = bufferedReader.readLine();
        User user = JSON.parseObject(json, User.class);

        if (userService.userRegister(user)) {
            json = JSON.toJSONString(true);
        }
        else {
            json = JSON.toJSONString(false);
        }
        response.getWriter().write(json);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
